package com.yidu.BusinessData.service.impl;

import com.google.gson.Gson;
import com.yidu.BusinessData.entity.CashAccrued;
import com.yidu.BusinessData.mapper.CashAccruedMapper;
import com.yidu.BusinessData.service.CashAccruedService;
import com.yidu.CashManagement.entity.FundsAllot;
import com.yidu.CashManagement.util.CreateId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周楠櫻木
 * @version 1.0
 * @date 2022/12/11
 */
@Service
public class CashAccruedServiceImpl implements CashAccruedService {

    @Autowired
    private CashAccruedMapper cashAccruedMapper;

    /**
     *  现金的查询
     * @param page
     * @param row
     * @param businessTime
     * @param businessType
     * @return
     */
    @Override
    public Map selectCashAccrued(int page, int row, String businessTime, int businessType) {
        String term = append(businessTime,businessType).toString();
        Map selectMap = new HashMap();
        selectMap.put("page", (page - 1) * row);
        selectMap.put("row", row);
        selectMap.put("term", term);

        Map layUiMap = new HashMap();
        layUiMap.put("data", cashAccruedMapper.selectCashAccrued(selectMap));
        layUiMap.put("msg", "");
        layUiMap.put("code", 0);
        layUiMap.put("count", cashAccruedMapper.selectCashAccruedCount(selectMap));

        return layUiMap;


    }

    /**
     * Id增长
     * @param cashAccrued
     * @return
     */
    @Override
    public int addCashAccrued(CashAccrued cashAccrued) {
            String id= CreateId.createId("FA");
            cashAccrued.setCashAccrued(id);
            return cashAccruedMapper.addCashAccrued(cashAccrued);

    }

    /**
     * 现金应收应付删除
     * @param myArr
     */
    @Override
    public void deleteCashAccrued(String myArr) {
            String[] array = new Gson().fromJson(myArr, String[].class);
            for (String Id : array) {
                cashAccruedMapper.deleteCashAccrued(Id);

        }
    }

    /**
     * 查询最大ID
     * @return
     */
    @Override
    public String maxSelectCashAccruedId() {
        return cashAccruedMapper.maxSelectCashAccruedId();
    }

    /**
     * 两费删除
     * @param cashAccrued
     * @return
     */
    @Override
    public int deleteCashAccrueds(CashAccrued cashAccrued) {
        return cashAccruedMapper.deleteCashAccrueds(cashAccrued);
    }

    /**
     * 两费查询
     * @param cashAccrued
     * @return
     */
    @Override
    public int selectCashAccrueds(String cashAccrued) {
        return cashAccruedMapper.selectCashAccrueds(cashAccrued);
    }

    /**
     * 修改
     * @param cashAccrued
     * @return
     */
    @Override
    public String updateCashAccrued(CashAccrued cashAccrued) {
        String msg = "";
        try {
            int i = cashAccruedMapper.updateCashAccrued(cashAccrued);
            if(i>=1){
                msg="ok";
            }
        } catch (Exception e) {
            msg = "error";
        }
        return msg;
    }


    /**
     * 拼接条件
     * @param businessTime 时间
     * @param businessType 类型
     */
    public StringBuffer append(String businessTime, int businessType) {
        StringBuffer term = new StringBuffer();
        if (businessTime != null && !businessTime.equals("")) {
            term.append(" and businessTime='").append(businessTime).append("'");
        }
        if (businessType > 0) {
            term.append(" and businessType=").append(businessType);
        }
        return term;
    }


}
